package com.hl.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hl.domain.OrderMain;
import com.hl.mapper.PaymentRecordMapper;
import com.hl.domain.PaymentRecord;
import com.hl.service.IPaymentRecordService;

/**
 * 收款记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@Service
public class PaymentRecordServiceImpl implements IPaymentRecordService 
{
    @Autowired
    private PaymentRecordMapper paymentRecordMapper;

    /**
     * 查询收款记录
     * 
     * @param id 收款记录主键
     * @return 收款记录
     */
    @Override
    public PaymentRecord selectPaymentRecordById(Integer id)
    {
        return paymentRecordMapper.selectPaymentRecordById(id);
    }

    /**
     * 查询收款记录列表
     * 
     * @param paymentRecord 收款记录
     * @return 收款记录
     */
    @Override
    public List<PaymentRecord> selectPaymentRecordList(PaymentRecord paymentRecord)
    {
        return paymentRecordMapper.selectPaymentRecordList(paymentRecord);
    }

    /**
     * 新增收款记录
     * 
     * @param paymentRecord 收款记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPaymentRecord(PaymentRecord paymentRecord)
    {
        int rows = paymentRecordMapper.insertPaymentRecord(paymentRecord);
        insertOrderMain(paymentRecord);
        return rows;
    }

    /**
     * 修改收款记录
     * 
     * @param paymentRecord 收款记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePaymentRecord(PaymentRecord paymentRecord)
    {
        // 根据支付记录中的订单号删除对应的订单主表记录
        // 注意：这里应该传递订单号字符串，而不是支付记录的ID
        if (paymentRecord.getOrderId() != null && !paymentRecord.getOrderId().isEmpty()) {
            paymentRecordMapper.deleteOrderMainByOrderId(paymentRecord.getOrderId());
        }
        insertOrderMain(paymentRecord);
        return paymentRecordMapper.updatePaymentRecord(paymentRecord);
    }

    /**
     * 批量删除收款记录
     * 
     * @param ids 需要删除的收款记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaymentRecordByIds(Integer[] ids)
    {
        paymentRecordMapper.deleteOrderMainByOrderIds(ids);
        return paymentRecordMapper.deletePaymentRecordByIds(ids);
    }

    /**
     * 删除收款记录信息
     * 
     * @param id 收款记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaymentRecordById(Integer id)
    {
        // 先查询支付记录获取订单号，然后删除对应的订单主表记录
        PaymentRecord paymentRecord = paymentRecordMapper.selectPaymentRecordById(id);
        if (paymentRecord != null && paymentRecord.getOrderId() != null && !paymentRecord.getOrderId().isEmpty()) {
            paymentRecordMapper.deleteOrderMainByOrderId(paymentRecord.getOrderId());
        }
        return paymentRecordMapper.deletePaymentRecordById(id);
    }

    /**
     * 新增商品订单信息
     * 
     * @param paymentRecord 收款记录对象
     */
    public void insertOrderMain(PaymentRecord paymentRecord)
    {
        List<OrderMain> orderMainList = paymentRecord.getOrderMainList();
        Integer id = paymentRecord.getId();
        if (StringUtils.isNotNull(orderMainList))
        {
            List<OrderMain> list = new ArrayList<OrderMain>();
            for (OrderMain orderMain : orderMainList)
            {
                orderMain.setOrderId(String.valueOf(id));
                list.add(orderMain);
            }
            if (list.size() > 0)
            {
                paymentRecordMapper.batchOrderMain(list);
            }
        }
    }

    /**
     * 获取支付状态选项（去重）
     * 
     * @return 支付状态列表
     */
    @Override
    public List<String> getPaymentStatusOptions()
    {
        return paymentRecordMapper.getPaymentStatusOptions();
    }

    /**
     * 获取支付方式选项（去重）
     * 
     * @return 支付方式列表
     */
    @Override
    public List<String> getPaymentMethodOptions()
    {
        return paymentRecordMapper.getPaymentMethodOptions();
    }

    /**
     * 根据订单号查询支付记录
     * 
     * @param orderId 订单号
     * @return 支付记录
     */
    @Override
    public PaymentRecord selectPaymentRecordByOrderId(String orderId)
    {
        return paymentRecordMapper.selectPaymentRecordByOrderId(orderId);
    }
}
